II 构建准备

宿主系统准备

Introduction

In this chapter, the host tools needed for building LFS are checked and, if necessary, installed. Then a partition which will host the LFS system is prepared. We will create the partition itself, create a file system on it, and mount it.

宿主机依赖

如下指定版本或更高

  • Bash-3.2 (/bin/sh should be a symbolic or hard link to bash)
  • Binutils-2.17 (Versions greater than 2.29 are not recommended as they have not been tested)
  • Bison-2.3 (/usr/bin/yacc should be a link to bison or small script that executes bison)
  • Bzip2-1.0.4
  • Coreutils-6.9
  • Diffutils-2.8.1
  • Findutils-4.2.31
  • Gawk-4.0.1 (/usr/bin/awk should be a link to gawk)
  • GCC-4.7 including the C++ compiler, g++ (Versions greater than 7.2.0 are not recommended as they have not been tested)
  • Glibc-2.11 (Versions greater than 2.26 are not recommended as they have not been tested)
  • Grep-2.5.1a
  • Gzip-1.3.12
  • Linux Kernel-3.2
  • M4-1.4.10
  • Make-3.81
  • Patch-2.5.4
  • Perl-5.8.8
  • Sed-4.1.5
  • Tar-1.22
  • Texinfo-4.7
  • Xz-5.0.0

检查脚本

cat > version-check.sh << "EOF"
#!/bin/bash
# Simple script to list version numbers of critical development tools
export LC_ALL=C
bash --version | head -n1 | cut -d" " -f2-4
MYSH=$(readlink -f /bin/sh)
echo "/bin/sh -> $MYSH"
echo $MYSH | grep -q bash || echo "ERROR: /bin/sh does not point to bash"
unset MYSH

echo -n "Binutils: "; ld --version | head -n1 | cut -d" " -f3-
bison --version | head -n1

if [ -h /usr/bin/yacc ]; then
  echo "/usr/bin/yacc -> `readlink -f /usr/bin/yacc`";
elif [ -x /usr/bin/yacc ]; then
  echo yacc is `/usr/bin/yacc --version | head -n1`
else
  echo "yacc not found"
fi

bzip2 --version 2>&1 < /dev/null | head -n1 | cut -d" " -f1,6-
echo -n "Coreutils: "; chown --version | head -n1 | cut -d")" -f2
diff --version | head -n1
find --version | head -n1
gawk --version | head -n1

if [ -h /usr/bin/awk ]; then
  echo "/usr/bin/awk -> `readlink -f /usr/bin/awk`";
elif [ -x /usr/bin/awk ]; then
  echo awk is `/usr/bin/awk --version | head -n1`
else
  echo "awk not found"
fi

gcc --version | head -n1
g++ --version | head -n1
ldd --version | head -n1 | cut -d" " -f2-  # glibc version
grep --version | head -n1
gzip --version | head -n1
cat /proc/version
m4 --version | head -n1
make --version | head -n1
patch --version | head -n1
echo Perl `perl -V:version`
sed --version | head -n1
tar --version | head -n1
makeinfo --version | head -n1
xz --version | head -n1

echo 'int main(){}' > dummy.c && g++ -o dummy dummy.c
if [ -x dummy ]
  then echo "g++ compilation OK";
  else echo "g++ compilation failed"; fi
rm -f dummy.c dummy
EOF

# 执行脚本
bash version-check.sh

阶段构建LFS

LFS被设计为在一个会话中构建, 整个过程中不会关机. 这并不意味着必须一次完成, 存在的问题是从不同的位置重新开始LFS, 某些程序可能必须得重新编译.

章节 1-4

在宿主系统完成, 当重新启动的时候, 需要注意

程序使用root用户完成, 在2.4节之后, 需要为root用户设置LFS环境

章节 5
  • 必须挂载 /mnt/lfs 分区.
  • 章节 5 的所有指令必须在 lfs 用户下执行. su - lfs.
  • The procedures in Section 5.3, “General Compilation Instructions” are critical. If there is any doubt about installing a package, ensure any previously expanded tarballs are removed, re-extract the package files, and complete all instructions in that section.
章节 6-8
  • /mnt/lfs必须被挂载
  • 当切换根目录, 必须为root用户设置LFS环境. 其他地方不需要使用LFS环境
  • The virtual file systems must be mounted. This can be done before or after entering chroot by changing to a host virtual terminal and, as root, running the commands in Section 6.2.2, “Mounting and Populating /dev” and Section 6.2.3, “Mounting Virtual Kernel File Systems”.

创建一个新分区

LFS通常安装在专用分区, 推荐使用一个空的分区用来构建LFS系统

最小系统需要6GB空间, 如果构建LFS系统成为一个基本Linux系统, 则需要安装一些额外的包, 提供20G空间比较合理. LFS系统不会使用这么多空间, 这些空间被用来临时存储, 以及LFS完成后添加额外的功能. 另外, 编译包的过程中需要大量的磁盘空间, 包安装之后, 可以回收.

记住分区的名字(比如, sdb1), 本书称之为LFS分区, 同时记住交换分区的名字, 这些名字之后会写到/etc/fstab文件

在分区上创建文件系统

ext4:

mkfs -v -t ext4 /dev/<xxx>

swap:

mkswap /dev/<yyy>

设置 $LFS 变量

本书中, LFS变量会多次使用, 在LFS构建之前, 确保LFS变量已经定义. 它用来设置你用来构建LFS系统的目录, 我们使用/mnt/lfs(最终用什么取决于你), 如果使用一个单独的分区, 这个目录将会是你的挂载点.

export LFS=/mnt/lfs
echo $LFS

另外, 确保LFS变量总是被设置, 可以将变量设置写入.bash_profile

挂载新分区

# 创建挂载点, 并挂载
root@ubuntu:~# mkdir -pv $LFS
mkdir: created directory '/mnt/lfs'
root@ubuntu:~# mount -v -t ext4 /dev/sdb1 $LFS
mount: /dev/sdb1 mounted on /mnt/lfs.

如果使用多个分区 (比如一个分区挂载 / 另一个挂载 /usr), 可以这样:

mkdir -pv $LFS
mount -v -t ext4 /dev/sdb1 $LFS
mkdir -v $LFS/usr
mount -v -t ext4 /dev/sdb2 $LFS/usr

如果有使用 swap 分区, 使用 swapon 命令, 确保正确使用:

root@ubuntu:~# swapon -v /dev/sdb3
swapon /dev/sdb3
swapon: /dev/sdb3: found swap signature: version 1d, page-size 4, same byte order
swapon: /dev/sdb3: pagesize=4096, swapsize=17518559232, devsize=17518559232

包和补丁

介绍

本章节包含一个用于构建基础Linux系统的包列表.

创建$LFS/sources目录, 用于存放源码包, 补丁, 并作为工作目录

使用root用户执行命令, 创建目录, 用于存放源代码

root@ubuntu:~# mkdir -v $LFS/sources
mkdir: created directory '/mnt/lfs/sources'

修改目录权限

root@ubuntu:~# chmod -v a+wt $LFS/sources
mode of '/mnt/lfs/sources' changed from 0755 (rwxr-xr-x) to 1777 (rwxrwxrwt)

下载包列表

root@ubuntu:~# wget http://www.linuxfromscratch.org/lfs/view/8.1/wget-list

通过wget, 指定文件, 下载所有包

wget --input-file=wget-list --continue --directory-prefix=$LFS/sources

LFS-7.0开始, 有一个校验文件md5sums, 我们可以用它来校验所有源码包的正确性.

将该文件下载到$LFS/sources目录, 执行下面命令

cd $LFS/sources
root@ubuntu:/mnt/lfs/sources# wget http://www.linuxfromscratch.org/lfs/view/8.1/md5sums
也可以直接到作者提供的镜像站点下载

http://www.linuxfromscratch.org/lfs/packages.html#packages -> http://mirrors-usa.go-parts.com/lfs/lfs-packages/ 下载对应版本需要的包

直接下载作者打包好的, 更方便, 比如http://mirrors-usa.go-parts.com/lfs/lfs-packages/lfs-packages-8.1.tar

下载完之后, 验证

pushd $LFS/sources
# 如果没有验证文件, 需要先下载
# wget http://www.linuxfromscratch.org/lfs/view/8.1/md5sums
md5sum -c md5sums
popd

最后的准备工作

创建$LFS/tools目录

所有第五章节编译的包都将会安装到$LFS/tools, 这只是一个临时目录, 最后完成LFS系统之后, 将被丢弃.

# 创建目录
mkdir -v $LFS/tools
# 创建一个软链接
ln -sv $LFS/tools /

添加LFS用户

groupadd lfs
useradd -s /bin/bash -g lfs -m -k /dev/null lfs
# 设置登录密码
passwd lfs
# 修改 $LFS/tools 属主
chown -v lfs $LFS/tools
# 修改 $LFS/sources 属主
chown -v lfs $LFS/sources
# 切换到 lfs 用户
su - lfs

设置环境变量

cat > ~/.bash_profile << "EOF"
exec env -i HOME=$HOME TERM=$TERM PS1='\u:\w\$ ' /bin/bash
EOF
cat > ~/.bashrc << "EOF"
set +h
umask 022
LFS=/mnt/lfs
LC_ALL=POSIX
LFS_TGT=$(uname -m)-lfs-linux-gnu
PATH=/tools/bin:/bin:/usr/bin
export LFS LC_ALL LFS_TGT PATH
EOF

生效

source ~/.bash_profile

关于 SBUs

Standard Build Unit (SBU), 标准构建单元, 用来测量构建安装单个包的时间.

SBU测量工作如下, 本书第五章第一个包编译的是Binutils, 编译该包所花的时间将作为SBU, 其他包编译所需时间可以根据该包得出一个相对值.

比如, 考虑到一个包编译时间为 4.5 SBUs. 这意味着, 如果系统花了10分钟编译这个包, 那么编译示例里面的包将需要45分钟, 幸运的是. 大多数其他包的编译时间少于Binutils.

一般来说, SBUs 并不完全准确, 因为编译时间会受很多因素影响, 包括宿主机 GCC 版本.

针对多核计算机, 编译的时候, 可以指定多进程编译, 使用

export MAKEFLAGS='-j 2'

或者, 编译的时候使用

make -j2

关于测试组件

很多包会提供对应测试组件, 运行测试组件, 可以知道包的编译情况. 有一些包编译之后, 进行测试是很有必要的, 比如: GCC, Binutils, Glibc等, 测试过程可能会花费不少时间, 但是强烈推荐进行测试.

构建一个临时系统

介绍

通用编译指令

对包的构建, 做一些约定.

有一些包在构建之前, 需要打补丁, 但只有要规避某些问题的时候才需要打补丁. 补丁通常在本章和下章使用. 因此, 不用担心指令执行之后, 似乎下载的补丁不见了. 在应用补丁的时候, 出现的警告消息也不用担心, 补丁仍然应用成功.

大多数软件编译过程中, 屏幕上会不断滚动一些警告信息, 这些信息可以忽略, 这些警告一般都是提示某些标准将被弃用等, 但并不是无效. C标准经常变化, 而有些包还是使用的旧的标准. 这不是问题, 只是一些警告的提示.

再次检查 LFS 环境变量:

echo $LFS

两个重要项

构建指令假设宿主机依赖, 包括软链接都设置正确:

  • 使用的shell为bash
  • sh 链接到 bash
  • /usr/bin/awk 链接到 gawk
  • /usr/bin/yacc 链接到 bison 或执行 bison 的脚本.

强调构建过程

  1. 将所有源代码和补丁放置在chroot环境可访问的目录, 例如 /mnt/lfs/sources/. 不要将源代码放在 /mnt/lfs/tools/.
  2. 切换到源代码路径
  3. 对于每个包
    1. 使用tar命令, 解压, 在第5章, 解压的时候确保你使用的是lfs用户
    2. 切换到解压后的包路径
    3. 跟随本书的指令, 进行编译
    4. 返回源代码路径
    5. 除非有其他指示, 否咋删除解压目录

Binutils-2.29 - Pass 1

Binutils 包含链接器, 汇编程序, 一起其他一些工具处理对象文件.

Installation of Cross Binutils

It is important that Binutils be the first package compiled because both Glibc and GCC perform various tests on the available linker and assembler to determine which of their own features to enable.

The Binutils documentation recommends building Binutils in a dedicated build directory:

mkdir -v build
cd build

SBU的测量数据, 是从配置开始, 到第一次安装, 总共花的时间, 即{ ./configure ... && ... && make install; }.

Now prepare Binutils for compilation:

../configure --prefix=/tools            \
             --with-sysroot=$LFS        \
             --with-lib-path=/tools/lib \
             --target=$LFS_TGT          \
             --disable-nls              \
             --disable-werror

The meaning of the configure options:

  • --prefix=/tools

    配置Binutils安装到 /tools 目录.

  • --with-sysroot=$LFS

    交叉编译, 告诉编译器在 $LFS 目录找需要的系统库.

  • --with-lib-path=/tools/lib

    This specifies which library path the linker should be configured to use.

  • --target=$LFS_TGT

    Because the machine description in the LFS_TGT variable is slightly different than the value returned by the config.guess script, this switch will tell the configurescript to adjust Binutil’s build system for building a cross linker.

  • --disable-nls

    This disables internationalization as i18n is not needed for the temporary tools.

  • --disable-werror

    This prevents the build from stopping in the event that there are warnings from the host’s compiler.

Continue with compiling the package:

make
# make -j2

编译完成, 通常我们需要测试, make test, 但是在早起阶段, 我们的测试组件还没有安装到指定位置 (Tcl, Expect, and DejaGNU) . The benefits of running the tests at this point are minimal since the programs from this first pass will soon be replaced by those from the second.

如果在 x86_64 的架构上构建, 创建软链接, 确保工具链的完整性:

case $(uname -m) in
  x86_64) mkdir -v /tools/lib && ln -sv lib /tools/lib64 ;;
esac

Install the package:

make install

Details on this package are located in Section 6.16.2, “Contents of Binutils.”

GCC-7.2.0 - Pass 1

包含C和C++编译器

GCC 依赖 GMP, MPFR 和 MPC. 解压每个包到gcc源代码目录, 并重命名, 编译gcc的时候, 构建程序会自动使用它们.

tar xf gcc-7.2.0.tar.xz
cd gcc-7.2.0
tar xf ../mpfr-3.1.5.tar.xz
mv -v mpfr-3.1.5 mpfr
tar xf ../gmp-6.1.2.tar.xz
mv -v gmp-6.1.2 gmp
tar xf ../mpc-1.0.3.tar.gz
mv -v mpc-1.0.3 mpc


lfs@ubuntu:/mnt/lfs/sources$ tar xf gcc-7.2.0.tar.xz
lfs@ubuntu:/mnt/lfs/sources$ cd gcc-7.2.0
lfs@ubuntu:/mnt/lfs/sources/gcc-7.2.0$ tar xf ../mpfr-3.1.5.tar.xz
lfs@ubuntu:/mnt/lfs/sources/gcc-7.2.0$ mv -v mpfr-3.1.5 mpfr
'mpfr-3.1.5' -> 'mpfr'
lfs@ubuntu:/mnt/lfs/sources/gcc-7.2.0$ tar xf ../gmp-6.1.2.tar.xz
lfs@ubuntu:/mnt/lfs/sources/gcc-7.2.0$ mv -v gmp-6.1.2 gmp
'gmp-6.1.2' -> 'gmp'
lfs@ubuntu:/mnt/lfs/sources/gcc-7.2.0$ tar xf ../mpc-1.0.3.tar.gz
lfs@ubuntu:/mnt/lfs/sources/gcc-7.2.0$ mv -v mpc-1.0.3 mpc
'mpc-1.0.3' -> 'mpc'

下面的命令将改变GCC默认动态链接器的位置到/tools, 同时会将/usr/include, 从gcc的搜索目录中移除.

for file in gcc/config/{linux,i386/linux{,64}}.h
do
  cp -uv $file{,.orig}
  sed -e 's@/lib\(64\)\?\(32\)\?/ld@/tools&@g' \
      -e 's@/usr@/tools@g' $file.orig > $file
  echo '
#undef STANDARD_STARTFILE_PREFIX_1
#undef STANDARD_STARTFILE_PREFIX_2
#define STANDARD_STARTFILE_PREFIX_1 "/tools/lib/"
#define STANDARD_STARTFILE_PREFIX_2 ""' >> $file
  touch $file.orig
done

最后, 在 x86_64 主机上, 设置 64-bit libraries 默认目录为 “lib”:

case $(uname -m) in
  x86_64)
    sed -e '/m64=/s/lib64/lib/' \
        -i.orig gcc/config/i386/t-linux64
 ;;
esac

The GCC documentation recommends building GCC in a dedicated build directory:

mkdir -v build
cd       build

Prepare GCC for compilation:

../configure                                       \
    --target=$LFS_TGT                              \
    --prefix=/tools                                \
    --with-glibc-version=2.11                      \
    --with-sysroot=$LFS                            \
    --with-newlib                                  \
    --without-headers                              \
    --with-local-prefix=/tools                     \
    --with-native-system-header-dir=/tools/include \
    --disable-nls                                  \
    --disable-shared                               \
    --disable-multilib                             \
    --disable-decimal-float                        \
    --disable-threads                              \
    --disable-libatomic                            \
    --disable-libgomp                              \
    --disable-libmpx                               \
    --disable-libquadmath                          \
    --disable-libssp                               \
    --disable-libvtv                               \
    --disable-libstdcxx                            \
    --enable-languages=c,c++

The meaning of the configure options:

--with-newlib

Since a working C library is not yet available, this ensures that the inhibit_libc constant is defined when building libgcc. This prevents the compiling of any code that requires libc support.

--without-headers

When creating a complete cross-compiler, GCC requires standard headers compatible with the target system. For our purposes these headers will not be needed. This switch prevents GCC from looking for them.

--with-local-prefix=/tools

The local prefix is the location in the system that GCC will search for locally installed include files. The default is /usr/local. Setting this to /tools helps keep the host location of /usr/local out of this GCC’s search path.

--with-native-system-header-dir=/tools/include

By default GCC searches /usr/include for system headers. In conjunction with the sysroot switch, this would translate normally to $LFS/usr/include. However the headers that will be installed in the next two sections will go to $LFS/tools/include. This switch ensures that gcc will find them correctly. In the second pass of GCC, this same switch will ensure that no headers from the host system are found.

--disable-shared

This switch forces GCC to link its internal libraries statically. We do this to avoid possible issues with the host system.

--disable-decimal-float, --disable-threads, --disable-libatomic, --disable-libgomp, --disable-libmpx, --disable-libquadmath, --disable-libssp, --disable-libvtv, --disable-libstdcxx

These switches disable support for the decimal floating point extension, threading, libatomic, libgomp, libmpx, libquadmath, libssp, libvtv, and the C++ standard library respectively. These features will fail to compile when building a cross-compiler and are not necessary for the task of cross-compiling the temporary libc.

--disable-multilib

On x86_64, LFS does not yet support a multilib configuration. This switch is harmless for x86.

--enable-languages=c,c++

This option ensures that only the C and C++ compilers are built. These are the only languages needed now.

Compile GCC by running:

make

Compilation is now complete. At this point, the test suite would normally be run, but, as mentioned before, the test suite framework is not in place yet. The benefits of running the tests at this point are minimal since the programs from this first pass will soon be replaced.

Install the package:

make install

Details on this package are located in Section 6.20.2, “Contents of GCC.”

Linux-4.12.7 API Headers

The Linux API Headers (in linux-4.12.7.tar.xz) expose the kernel’s API for use by Glibc.

Approximate build time:less than 0.1 SBU

Required disk space:861 MB

Installation of Linux API Headers

The Linux kernel needs to expose an Application Programming Interface (API) for the system’s C library (Glibc in LFS) to use. This is done by way of sanitizing various C header files that are shipped in the Linux kernel source tarball.

Make sure there are no stale files embedded in the package:

make mrproper

Now extract the user-visible kernel headers from the source. They are placed in an intermediate local directory and copied to the needed location because the extraction process removes any existing files in the target directory.

make INSTALL_HDR_PATH=dest headers_install
cp -rv dest/include/* /tools/include

Details on this package are located in Section 6.7.2, “Contents of Linux API Headers.”

Glibc-2.26

The Glibc package contains the main C library. This library provides the basic routines for allocating memory, searching directories, opening and closing files, reading and writing files, string handling, pattern matching, arithmetic, and so on.

Approximate build time:4.2 SBU

Required disk space:790 MB

Installation of Glibc

The Glibc documentation recommends building Glibc in a dedicated build directory:

mkdir -v build
cd       build

Next, prepare Glibc for compilation:

../configure                             \
      --prefix=/tools                    \
      --host=$LFS_TGT                    \
      --build=$(../scripts/config.guess) \
      --enable-kernel=3.2             \
      --with-headers=/tools/include      \
      libc_cv_forced_unwind=yes          \
      libc_cv_c_cleanup=yes

The meaning of the configure options:

  • –host=:math:`LFS_TGT, –build=`(../scripts/config.guess)

    The combined effect of these switches is that Glibc’s build system configures itself to cross-compile, using the cross-linker and cross-compiler in /tools.

  • --enable-kernel=3.2

    This tells Glibc to compile the library with support for 3.2 and later Linux kernels. Workarounds for older kernels are not enabled.

  • –with-headers=/tools/include

    This tells Glibc to compile itself against the headers recently installed to the tools directory, so that it knows exactly what features the kernel has and can optimize itself accordingly.

  • libc_cv_forced_unwind=yes

    The linker installed during Section 5.4, “Binutils-2.29 - Pass 1” was cross-compiled and as such cannot be used until Glibc has been installed. This means that the configure test for force-unwind support will fail, as it relies on a working linker. The libc_cv_forced_unwind=yes variable is passed in order to inform configure that force-unwind support is available without it having to run the test.

  • libc_cv_c_cleanup=yes

    Similarly, we pass libc_cv_c_cleanup=yes through to the configure script so that the test is skipped and C cleanup handling support is configured.

During this stage the following warning might appear:

configure: WARNING:
*** These auxiliary programs are missing or
*** incompatible versions: msgfmt
*** some features will be disabled.
*** Check the INSTALL file for required versions.

The missing or incompatible msgfmt program is generally harmless. This msgfmt program is part of the Gettext package which the host distribution should provide.

Note
There have been reports that this package may fail when building as a “parallel make”. If this occurs, rerun the make command with a “-j1” option.

Compile the package:

make

Install the package:

make install

Caution

At this point, it is imperative to stop and ensure that the basic functions (compiling and linking) of the new toolchain are working as expected. To perform a sanity check, run the following commands:

echo 'int main(){}' > dummy.c
$LFS_TGT-gcc dummy.c
readelf -l a.out | grep ': /tools'

If everything is working correctly, there should be no errors, and the output of the last command will be of the form:

[Requesting program interpreter: /tools/lib/ld-linux.so.2]

Note that for 64-bit machines, the interpreter name will be /tools/lib64/ld-linux-x86-64.so.2.

If the output is not shown as above or there was no output at all, then something is wrong. Investigate and retrace the steps to find out where the problem is and correct it. This issue must be resolved before continuing on.

Once all is well, clean up the test files:

rm -v dummy.c a.out

Note

Building Binutils in the section after next will serve as an additional check that the toolchain has been built properly. If Binutils fails to build, it is an indication that something has gone wrong with the previous Binutils, GCC, or Glibc installations.

Details on this package are located in Section 6.9.3, “Contents of Glibc.”

Libstdc++-7.2.0

Libstdc++ is the standard C++ library. It is needed for the correct operation of the g++ compiler.

Approximate build time:0.4 SBU

Required disk space:750 MB

Installation of Target Libstdc++

Note

Libstdc++ is part of the GCC sources. You should first unpack the GCC tarball and change to the gcc-7.2.0 directory.

Create a separate build directory for Libstdc++ and enter it:

mkdir -v build
cd       build

Prepare Libstdc++ for compilation:

../libstdc++-v3/configure           \
    --host=$LFS_TGT                 \
    --prefix=/tools                 \
    --disable-multilib              \
    --disable-nls                   \
    --disable-libstdcxx-threads     \
    --disable-libstdcxx-pch         \
    --with-gxx-include-dir=/tools/$LFS_TGT/include/c++/7.2.0

The meaning of the configure options:

  • –host=…

    Indicates to use the cross compiler we have just built instead of the one in /usr/bin.

  • –disable-libstdcxx-threads

    Since we have not yet built the C threads library, the C++ one cannot be built either.

  • –disable-libstdcxx-pch

    This switch prevents the installation of precompiled include files, which are not needed at this stage.

  • –with-gxx-include-dir=/tools/$LFS_TGT/include/c++/7.2.0

    This is the location where the standard include files are searched by the C++ compiler. In a normal build, this information is automatically passed to the Libstdc++ configure options from the top level directory. In our case, this information must be explicitly given.

Compile libstdc++ by running:

make

Install the library:

make install

Details on this package are located in Section 6.20.2, “Contents of GCC.”

Binutils-2.29 - Pass 2

The Binutils package contains a linker, an assembler, and other tools for handling object files.

Approximate build time:1.1 SBU

Required disk space:582 MB

Installation of Binutils

Create a separate build directory again:

mkdir -v build
cd       build

Prepare Binutils for compilation:

CC=$LFS_TGT-gcc                \
AR=$LFS_TGT-ar                 \
RANLIB=$LFS_TGT-ranlib         \
../configure                   \
    --prefix=/tools            \
    --disable-nls              \
    --disable-werror           \
    --with-lib-path=/tools/lib \
    --with-sysroot

The meaning of the new configure options:

  • CC=:math:`LFS_TGT-gcc AR=`LFS_TGT-ar RANLIB=$LFS_TGT-ranlib

    Because this is really a native build of Binutils, setting these variables ensures that the build system uses the cross-compiler and associated tools instead of the ones on the host system.

  • –with-lib-path=/tools/lib

    This tells the configure script to specify the library search path during the compilation of Binutils, resulting in /tools/lib being passed to the linker. This prevents the linker from searching through library directories on the host.

  • –with-sysroot

    The sysroot feature enables the linker to find shared objects which are required by other shared objects explicitly included on the linker’s command line. Without this, some packages may not build successfully on some hosts.

Compile the package:

make

Install the package:

make install

Now prepare the linker for the “Re-adjusting” phase in the next chapter:

make -C ld clean
make -C ld LIB_PATH=/usr/lib:/lib
cp -v ld/ld-new /tools/bin

The meaning of the make parameters:

  • -C ld clean

    This tells the make program to remove all compiled files in the ld subdirectory.

  • -C ld LIB_PATH=/usr/lib:/lib

    This option rebuilds everything in the ld subdirectory. Specifying the LIB_PATH Makefile variable on the command line allows us to override the default value of the temporary tools and point it to the proper final path. The value of this variable specifies the linker’s default library search path. This preparation is used in the next chapter.

Details on this package are located in Section 6.16.2, “Contents of Binutils.”